home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / astrolog / src / placalc.c < prev    next >
C/C++ Source or Header  |  1995-08-11  |  47KB  |  1,465 lines

  1. /*                                                               -*- C -*-
  2. ** Astrolog (Version 4.40) File: placalc.c
  3. **
  4. ** IMPORTANT NOTICE: The graphics database and chart display routines
  5. ** used in this program are Copyright (C) 1991-1995 by Walter D. Pullen
  6. ** (astara@u.washington.edu). Permission is granted to freely use and
  7. ** distribute these routines provided one doesn't sell, restrict, or
  8. ** profit from them in any way. Modification is allowed provided these
  9. ** notices remain with any altered or edited versions of the program.
  10. **
  11. ** The main planetary calculation routines used in this program have
  12. ** been Copyrighted and the core of this program is basically a
  13. ** conversion to C of the routines created by James Neely as listed in
  14. ** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
  15. ** available from Matrix Software. The copyright gives us permission to
  16. ** use the routines for personal use but not to sell them or profit from
  17. ** them in any way.
  18. **
  19. ** The PostScript code within the core graphics routines are programmed
  20. ** and Copyright (C) 1992-1993 by Brian D. Willoughby
  21. ** (brianw@sounds.wa.com). Conditions are identical to those above.
  22. **
  23. ** The extended accurate ephemeris databases and formulas are from the
  24. ** calculation routines in the program "Placalc" and are programmed and
  25. ** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
  26. ** (alois@azur.ch). The use of that source code is subject to
  27. ** regulations made by Astrodienst Zurich, and the code is not in the
  28. ** public domain. This copyright notice must not be changed or removed
  29. ** by any user of this program.
  30. **
  31. ** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
  32. ** X Window graphics initially programmed 10/23-29/1991.
  33. ** PostScript graphics initially programmed 11/29-30/1992.
  34. ** Last code change made 1/29/1995.
  35. */
  36.  
  37. #include "placalc.h"
  38.  
  39. /* dokumentiert von Tobias Ferber, <tf@ganymed.hall.sub.org> */
  40.  
  41. #ifdef PLACALC
  42. /*
  43. ** ---------------------------------------------------------------
  44. ** | Copyright Astrodienst AG and Alois Treindl, 1989,1991,1993  |
  45. ** | The use of this source code is subject to regulations made  |
  46. ** | by Astrodienst Zurich. The code is NOT in the public domain.|
  47. ** |                                                             |
  48. ** | This copyright notice must not be changed or removed        |
  49. ** | by any user of this program.                                |
  50. ** ---------------------------------------------------------------
  51. **
  52. ** Important changes:
  53. ** 11-jun-93 revision 1.12: fixed error which affected Mercury between -2100
  54. ** and -3100 (it jumped wildly).
  55. */
  56.  
  57. /************************************************************
  58. externally accessible globals, defined as extern in placalc.h
  59. ************************************************************/
  60.  
  61. REAL8 meanekl, ekl, nut;
  62. struct elements el[MARS + 1];
  63.  
  64. /*
  65. ** The global variable ephe_path indicates where the ephemeris files
  66. ** LRZ5_xx and CHI_xx are stored.
  67. ** By default it is set to the #defined EPHE_PATH, but the user of the
  68. ** placalc module can change it to any other location before he
  69. ** starts calling calc().
  70. */
  71.  
  72. char *ephe_path = EPHE_PATH;
  73.  
  74.  
  75. #ifdef ASTROLOG
  76.  
  77. /* Given an object index and a Julian Day time, get its zodiac and        */
  78. /* declination position (planetary longitude and latitude) of the object  */
  79. /* and its velocity and distance from the Earth or Sun. This basically    */
  80. /* just calls the Placalc calculation function to actually do it, but as  */
  81. /* this is the one routine called from Astrolog, this is the one routine  */
  82. /* which has knowledge of and uses both Astrolog and Placalc definitions, */
  83. /* and does things such as translation to Placalc indices and formats.    */
  84.  
  85. bool FPlacalcPlanet(ind, jd, helio, planet, planetalt, ret, space)
  86. int ind, helio;
  87. double jd;
  88. real *planet, *planetalt, *ret, *space;
  89. {
  90.   int iplanet, flag;
  91.   REAL8 jd_ad, rlng, rrad, rlat, rspeed;
  92.  
  93.   if (ind <= oPlu)      /* Convert Astrolog object index to Placalc index. */
  94.     iplanet = ind-1;
  95.   else if (ind == oChi)
  96.     iplanet = CHIRON;
  97.   else if (ind == oNod)
  98.     iplanet = us.fTrueNode ? TRUE_NODE : MEAN_NODE;
  99.   else if (ind == oLil)
  100.     iplanet = LILITH;
  101.   else
  102.     return fFalse;
  103.  
  104.   jd_ad = jd - JUL_OFFSET;
  105.   flag = helio ? CALC_BIT_SPEED | CALC_BIT_HELIO : CALC_BIT_SPEED;
  106.   jd_ad += deltat(jd_ad);
  107.  
  108.   if (calc(iplanet, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK)
  109.   {
  110.     *planet    = rlng;
  111.     *planetalt = rlat;
  112.     *ret       = rspeed;
  113.     *space     = rrad;
  114.     return fTrue;
  115.   }
  116.   return fFalse;
  117. }
  118. #endif /* ASTROLOG */
  119.  
  120.  
  121. /* function calc(): 
  122. ** This is the main routine for computing a planets position.
  123. ** The function has several modes, which are controlled by bits in
  124. ** the parameter 'flag'. The normal mode (flag == 0) computes
  125. ** a planets apparent geocentric position in ecliptic coordinates relative to
  126. ** the true equinox of date, without speed
  127. ** 
  128. ** Explanation of the arguments: see the functions header.
  129. ** 
  130. ** Returns OK or ERR (if some planet out of time range). OK and ERR are
  131. ** defined in ourdef.h and must not be confused with TRUE and FALSE.
  132. ** OK and ERR are of type int, not of type BOOLEAN.
  133. ** 
  134. ** Bits used in flag:
  135. ** CALC_BIT_HELIO     0 = geocentric, 1 = heliocentric
  136. ** CALC_BIT_NOAPP       0 = apparent positions, 1 = true positions
  137. ** CALC_BIT_NONUT     0 = do nutation (true equinox of date)
  138. ** 1 = don't do nutation (mean equinox of date).
  139. ** 
  140. ** CALC_BIT_SPEED     0 = don't calc speed,
  141. ** 1 = calc speed, takes quite long for moon
  142. ** (is observed only for moon, with other
  143. ** planets speed is cheap)
  144. ** 
  145. ** Side effects and local memory:
  146. ** For doing heliocentric positions the fucntion must know the
  147. ** earth's position for the desired time t. It remembers the earth
  148. ** position so it does not have to recompute it each time a planet
  149. ** position is wanted for the same time t.
  150. ** It calls helup(t), which leaves as a side effect the global
  151. ** variables meanekl, ekl and nut for the time t.
  152. ** 
  153. ** Functions called by calc():
  154. ** helup(t)
  155. ** hel(t)
  156. ** moon(t)
  157. ** togeo()
  158. ** 
  159. ** Time range:
  160. ** The function can be used savely in the time range 5000 BC to
  161. ** 3000 AD. The stored ephemeris is available only for this time
  162. ** range, so Jupiter ... Pluto cannot be computed outside. The
  163. ** function will return results for the other planets also outside
  164. ** of this time range, but they become meaningless pretty soon
  165. ** before 5000 BC, because Newcombs time series expansions for the
  166. ** elements will not work anymore.
  167. ** 
  168. ** pointers to the return variables:
  169. ** alng = ecliptic longitude in degrees
  170. ** arad = radius vector in AU (astronomic units)
  171. ** alat = ecliptic latitude in degrees
  172. ** alngspeed = speed of planet in degrees per day
  173. **
  174. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  175. ** The precision of the speed is quite limited.
  176. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  177. ** 
  178. ** For Sun, Mercury, Venus and Mars we take only the speed from
  179. ** the undisturbed Kepler orbit. For the Moon there is no
  180. ** reasonable undisturbed orbit and we derive the speed from
  181. ** its position at t + dt and t - dt. We need these 
  182. ** moon positions anyway for the true node calculation.
  183. ** For the outer planets and Chiron we derive the precise
  184. ** speed from the stored ephemeris by high order inter-
  185. ** polation; the precision is limited for the geocentric
  186. ** case due to the limited precision of the earth's/sun's speed.
  187. ** Applications who need precise speeds should
  188. ** get them by calling calc() with slightly different times.
  189. ** 
  190. ** Comment 7 May 1991 by Alois Treindl:
  191. ** Center of Earth versus Barycenter Earth-Moon:
  192. ** Brown's theory of the moon gives the moon's coordinates relative
  193. ** to the center of the earth. Newcomb's theory of the Sun gives the
  194. ** coordinates of the earth's center relative to the center of the Sun.
  195. ** This is what we need.
  196. **
  197. ** How about the Mean Lunar Node?
  198. ** The orbital elements of the Sun in Newcomb's theory are given
  199. ** relative to the barycenter Earth-Moon; the reduction to geocentric
  200. ** is only applied after doing the Kepler ellipse calculation.
  201. ** Are the Lunar elements also relative to the barycenter??
  202. ** If yes:
  203. ** When we use the moon's mean node out of the elements, it is still
  204. ** as seen from the barycenter. Because the node is close to the
  205. ** earth, we would have to apply a considerable correction, which is of
  206. ** the order of 4000/384000 km or 35' (minutes of arc).
  207. ** Nobody has ever applied such a correction to the mean node.
  208. **
  209. ** And the True Node?
  210. ** When we calculate the osculating orbital elements of the Moon (true node),
  211. ** are they relative to the barycenter or to the Earth's center?
  212. ** Our derivation of true node from the actual Moon positions considers
  213. ** the earth's center as the focal point of the osculating lunar ellipse.
  214. ** A more correct approach would first reduce the lunar position from
  215. ** geocentric to barycentric, then compute the orbital elements from
  216. ** the reduced positions, and then reduce the desired items
  217. ** (node, apogaeum, 'dark moon') to geocentric positions.
  218. ** No known astrological ephemeris has ever used such a correction, which is
  219. ** of the same order of magnitude as the correction to the meannode above.
  220. ** When the moon is going through the ecliptic, the geocenter, barycenter
  221. ** moon (and the node identical to the moon itself) line up; this is why
  222. ** the error does not show up in normal considerations.
  223. */
  224.  
  225. int calc(planet, jd_ad, flag, alng, arad, alat, alngspeed)
  226. int planet;  /* planet index as defined in placalc.h,
  227. SUN = 0, MOON = 1 etc.
  228. planet == -1 calc calculates only nut and ecl */
  229. REAL8 jd_ad;  /* relative Astrodienst Juldate, ephemeris time.
  230. Astrodienst Juldate is relative 31 Dec 1949, noon. */
  231. int flag;  /* See definition of flag bits above */
  232. REAL8 *alng;
  233. REAL8 *arad;
  234. REAL8 *alat;
  235. REAL8 *alngspeed;
  236. {
  237.   struct rememberdat  /* time for which the datas are calculated */
  238.     {REAL8 calculation_time, lng, rad, zet, lngspeed, radspeed, zetspeed;};
  239.  
  240.   static struct rememberdat earthrem =
  241.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  242.  
  243.   static struct rememberdat moonrem  =
  244.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  245.  
  246.   REAL8 c, s, x, knn, knv;
  247.   REAL8 rp, zp; /* needed to call hel! */
  248.   REAL8 *azet = alat;
  249.  
  250.   BOOLEAN calc_geo, calc_helio, calc_apparent, calc_speed,
  251.   calc_nut;
  252.  
  253.   /* helup checks whether it was already called with same time */
  254.   helup (jd_ad);
  255.   if (planet == CALC_ONLY_ECL_NUT)
  256.     return OK;
  257.  
  258.   calc_helio =  flag & CALC_BIT_HELIO;
  259.   calc_geo = ! calc_helio;
  260.   calc_apparent = ! (flag & CALC_BIT_NOAPP);
  261.   calc_nut = ! (flag & CALC_BIT_NONUT);
  262.   calc_speed = flag & CALC_BIT_SPEED;
  263.  
  264.   /*
  265.   ** it is necessary to compute EARTH in the following cases:
  266.   ** heliocentric MOON or EARTH
  267.   ** geocentric any planet except MOON or nodes or LILITH
  268.   */
  269.  
  270.   if( (calc_helio && (planet == MOON || planet == EARTH))
  271.   ||  (calc_geo   && (planet != MOON && planet != MEAN_NODE
  272.                                      && planet != TRUE_NODE
  273.                                      && planet != LILITH)) )
  274.   {
  275.     if (earthrem.calculation_time != jd_ad) 
  276.     {
  277.       hel(EARTH, jd_ad, alng, arad, azet, alngspeed, &rp, &zp);
  278.  
  279.       /* store earthdata for geocentric calculation: */
  280.  
  281.       earthrem.lng = *alng;
  282.       earthrem.rad = *arad;
  283.       earthrem.zet = *azet;
  284.       earthrem.lngspeed = *alngspeed;
  285.       earthrem.radspeed = rp;
  286.       earthrem.zetspeed = zp;
  287.       earthrem.calculation_time = jd_ad;
  288.     }
  289.   }
  290.   switch(planet) {
  291.  
  292.   case EARTH: /* has been already computed */
  293.     *alng = earthrem.lng;
  294.     *arad = earthrem.rad;
  295.     *azet = earthrem.zet;
  296.     *alngspeed = earthrem.lngspeed;
  297.     rp = earthrem.radspeed;
  298.     zp = earthrem.zetspeed;
  299.     if (calc_geo) { /* SUN seen from earth */
  300.       *alng = smod8360(*alng + 180.0);  
  301.       *azet = - *azet;
  302.     }
  303.     if (calc_apparent)
  304.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  305.     break;
  306.  
  307.   case MOON:
  308.     moon(alng, arad, azet);  
  309.     moonrem.lng = *alng;  /* moonrem will be used for TRUE_NODE */
  310.     moonrem.rad = *arad;
  311.     moonrem.zet = *azet;
  312.     *alngspeed = 12;
  313.     moonrem.calculation_time = jd_ad;
  314.     if (calc_helio || calc_speed) {/* get a second moon position */
  315.       REAL8 lng2, rad2, zet2;
  316.       helup(jd_ad + MOON_SPEED_INTERVAL);   
  317.       moon(&lng2, &rad2, &zet2);
  318.       helup(jd_ad);
  319.       if (calc_helio) { /* moon as seen from sun */
  320.         togeo(earthrem.lng, -earthrem.rad, moonrem.lng, moonrem.rad,
  321.         moonrem.zet, alng, arad);
  322.         togeo(earthrem.lng + MOON_SPEED_INTERVAL * earthrem.lngspeed, 
  323.         -(earthrem.rad + MOON_SPEED_INTERVAL * earthrem.radspeed), 
  324.         lng2, rad2, zet2, &lng2, &rad2);
  325.       }
  326.       *alngspeed =  diff8360(lng2, *alng) / MOON_SPEED_INTERVAL;
  327.       /* rp = (rad2 - *arad) / MOON_SPEED_INTERVAL;       */
  328.       /* zp = (zet2 - moonrem.zet) / MOON_SPEED_INTERVAL; */
  329.     }
  330.     *alat = RADTODEG * ASIN8(*azet / *arad);
  331.  
  332.     /*
  333.     ** light time correction, not applied for moon or nodes;
  334.     ** moon would have only term of ca. 0.02", see Expl.Sup.1961 p.109
  335.     */
  336.  
  337.     break;
  338.  
  339.   case MERCURY:
  340.   case VENUS:
  341.   case MARS:
  342.   case JUPITER:
  343.   case SATURN:
  344.   case URANUS:
  345.   case NEPTUNE:
  346.   case PLUTO:
  347.   case CHIRON:
  348.     if(hel (planet, jd_ad, alng, arad, azet, alngspeed, &rp, &zp) != OK)
  349.       return ERR; /* outer planets can fail if out of ephemeris range */
  350.  
  351.     if(calc_geo)    /* geocentric */
  352.     {
  353.       REAL8 lng1, rad1, lng2, rad2;
  354.       togeo(earthrem.lng, earthrem.rad, *alng, *arad, *azet, &lng1, &rad1);
  355.       togeo(earthrem.lng + earthrem.lngspeed, 
  356.       earthrem.rad + earthrem.radspeed, 
  357.       *alng + *alngspeed, *arad + rp, *azet + zp, &lng2, &rad2);
  358.       *alng = lng1;
  359.       *arad = rad1; 
  360.       *alngspeed = diff8360(lng2, lng1);
  361.       /* rp = rad2 - rad1; */
  362.     }
  363.     *alat = RADTODEG * ASIN8(*azet / *arad);
  364.     if (calc_apparent)
  365.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  366.     break;
  367.  
  368.   case MEAN_NODE:
  369.     *alng = smod8360(el[MOON].kn);
  370.     /*
  371.     * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  372.     * Our current use of the axis a is wrong, but is never used.
  373.     */
  374.     *arad = pd[MOON].axis;
  375.     *alat = 0.0;
  376.     *alngspeed = -0.053;
  377.     break;
  378.  
  379.   case TRUE_NODE: {
  380.     /* see comment 'Note 7 May 1991' above */
  381.     REAL8 ln, rn, zn, 
  382.     lv, rv, zv, 
  383.     l1, r1, z1,
  384.     xn, yn, xv, yv, r0, x0, y0;
  385.  
  386.     helup(jd_ad + NODE_INTERVAL);
  387.     moon(&ln, &rn, &zn);
  388.     helup(jd_ad - NODE_INTERVAL);
  389.     moon(&lv, &rv, &zv);
  390.     helup(jd_ad);
  391.  
  392.     if (moonrem.calculation_time != jd_ad)
  393.       moon(&l1, &r1, &z1);
  394.  
  395.     else /* moon is already calculated */
  396.     {
  397.       l1 = moonrem.lng;
  398.       r1 = moonrem.rad;
  399.       z1 = moonrem.zet;
  400.     }
  401.     rn = sqrt(rn * rn - zn * zn);
  402.     rv = sqrt(rv * rv - zv * zv);
  403.     r0 = sqrt(r1 * r1 - z1 * z1);
  404.     xn = rn * COS8(DEGTORAD * ln);
  405.     yn = rn * SIN8(DEGTORAD * ln);
  406.     xv = rv * COS8(DEGTORAD * lv);
  407.     yv = rv * SIN8(DEGTORAD * lv);
  408.     x0 = r0 * COS8(DEGTORAD * l1);   
  409.     y0 = r0 * SIN8(DEGTORAD * l1);   
  410.     x = test_near_zero(x0 * yn - xn * y0);
  411.     s = (y0 * zn - z1 * yn) / x;
  412.     c = test_near_zero((x0 * zn - z1 * xn) / x);
  413.     knn =  smod8360(RADTODEG * ATAN28(s, c)); /* = ATAN8(s / c) */
  414.     x = test_near_zero(y0 * xv - x0 * yv);
  415.     s = (yv * z1 - zv * y0) / x;
  416.     c = test_near_zero((xv * z1 - zv * x0) / x);
  417.     knv =  smod8360(RADTODEG * ATAN28(s, c));        
  418.     *alng = smod8360((knv + knn) / 2);
  419.  
  420.     /*
  421.     ** the distance of the node is the 'orbital parameter' p = a (1-e^2);
  422.     ** Our current use of the axis a is wrong.
  423.     */
  424.  
  425.     *arad = pd[MOON].axis;
  426.     *alat = 0.0;
  427.     *alngspeed = diff8360(knn, knv) / NODE_INTERVAL;
  428.     }
  429.     break;
  430.  
  431.   case LILITH: {
  432.     /*
  433.     ** Added 22-Jun-93
  434.     ** Lilith or Dark Moon is the empty focal point of the mean lunar ellipse.
  435.     ** This is 180 degrees from the perihel.
  436.     ** Because the lunar orbit is not in the ecliptic, it must be
  437.     ** projected onto the ecliptic in the same way as the planetary orbits
  438.     ** are (see for example Montenbruck, Grundlagen der Ephemeridenrechnung).
  439.     **
  440.     ** We compute the MEAN Lilith, not the TRUE one which would have to be
  441.     ** derived in a similar way as the true node.
  442.     ** For the radius vector of Lilith we use a simple formula; 
  443.     ** to get a precise value, the fact that the focal point of the ellipse
  444.     ** is not at the center of the earth but at the barycenter moon-earth
  445.     ** would have to be accounted for.
  446.     ** For the speed we always return a constant: the T term from the
  447.     ** lunar perihel.
  448.     ** Joelle de Gravelaine publishes in her book "Lilith der schwarze Mond"
  449.     ** (Astrodata, 1990) an ephemeris which gives noon (12.00) positions
  450.     ** but does not project onto the ecliptic.
  451.     ** This creates deviations
  452.     */
  453.     double arg_lat, lon, cosi;
  454.     struct elements *e = &el[MOON];
  455.     arg_lat = degnorm(e->pe - e->kn + 180.0);
  456.     cosi = COSDEG(e->in);
  457.  
  458.     if (e->in == 0 || ABS8(arg_lat -  90.0) < TANERRLIMIT
  459.                    || ABS8(arg_lat - 270.0) < TANERRLIMIT) 
  460.     {
  461.       lon = arg_lat;
  462.     } 
  463.  
  464.     else
  465.     {
  466.       lon = ATAN8(TANDEG(arg_lat) * cosi);
  467.       lon = RADTODEG * lon;
  468.       if (arg_lat > 90.0 && arg_lat < 270.0)  lon += 180.0;
  469.     }
  470.  
  471.     lon = degnorm(lon + e->kn);
  472.     *alng = lon;
  473.     *alngspeed = 0.111404;  /* 6'41.05" per day */
  474.     *arad = 2 * pd[MOON].axis * e->ex;
  475.  
  476.     /*
  477.     ** To test Gravalaines error, return unprojected long in alat.
  478.     ** the correct latitude would be:
  479.     ** *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  480.     */
  481.  
  482. #ifdef ASTROLOG
  483.     *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  484. #else
  485.     *alat = degnorm(arg_lat + e->kn); /* unprojected longitude, no nut */
  486. #endif
  487.  
  488.     }
  489.     break;
  490.  
  491.   default:
  492.     return ERR;
  493.   } /* end switch */ 
  494.  
  495.   if (calc_nut)
  496.     *alng += nut;
  497.  
  498.   *alng = smod8360(*alng);  /* normalize to circle */
  499.   return OK;
  500. }
  501.  
  502.  
  503. /*
  504. ** get relative earth distance in range 0..1000:
  505. ** To compute the relative distance we use fixed values of 
  506. ** mimimum and maximum distance measured empirically between
  507. ** 1300 AD and 2300 AD (see helconst.c). 
  508. ** This approach is certainly fine for the
  509. ** outer planets, but not the best for Sun and Moon, where it
  510. ** would be better to look at the mean anomaly, i.e. the progress
  511. ** the planet makes on it's Kepler orbit.
  512. ** Considering the low importance astrologers give to the relative
  513. ** distance, we consider the effort not worthwile.
  514. ** Now we compare real radius with longtime-averaged distances.
  515. */
  516.  
  517. int rel_geo(planet, rau)
  518. int planet;
  519. double rau;
  520. {
  521.   int rgeo;
  522.  
  523.   if (planet == MEAN_NODE || planet == TRUE_NODE || planet == LILITH) 
  524.   {
  525.     return 0;
  526.   } 
  527.   else 
  528.   {
  529.     rgeo = 1000 * (int)(1.0 - (rau - rmima[planet][0]) / (rmima[planet][1] - rmima[planet][0]));
  530.   }
  531.  
  532.   if (rgeo < 0)
  533.     rgeo = 0;
  534.  
  535.   else if (rgeo > 999)
  536.     rgeo = 999;
  537.  
  538.   return rgeo;
  539. }
  540.  
  541.  
  542. /* helio to geocentric conversion */
  543.  
  544. void togeo(lngearth, radearth, lng, rad, zet, alnggeo, aradgeo)
  545. REAL8 lngearth;
  546. REAL8 radearth;
  547. REAL8 lng;
  548. REAL8 rad;
  549. REAL8 zet;
  550. REAL8 *alnggeo;
  551. REAL8 *aradgeo;
  552. {
  553.   REAL8 r1, x, y;
  554.  
  555.   r1 = sqrt(rad * rad - zet * zet);
  556.   x = r1 * COS8(DEGTORAD * lng) - radearth * COS8(DEGTORAD * lngearth);
  557.   y = r1 * SIN8(DEGTORAD * lng) - radearth * SIN8(DEGTORAD * lngearth);
  558.   *aradgeo = sqrt(x * x + y * y + zet * zet);
  559.   x = test_near_zero(x);
  560.   *alnggeo = smod8360(RADTODEG * ATAN28(y, x));
  561. }
  562.  
  563.  
  564. /*
  565. ** helup()
  566. ** prepares the orbital elements and the disturbation arguments for the
  567. ** inner planets and the moon. helup(t) is called by hel() and by calc().
  568. ** helup() returns its results in global variables.
  569. ** helup() remembers the t it has been called with before and does
  570. ** not recalculate its results when it is called more than once with
  571. ** the same t.
  572. */
  573.  
  574. void helup(jd_ad)
  575. REAL8 jd_ad;
  576. {
  577.   int i;
  578.   static REAL8 thelup = HUGE8;  /* is initialized only once at load time */
  579.   struct elements *e = el;      /* pointer to el[i] */  
  580.   struct elements *ee = el;     /* pointer to el[EARTH] */  
  581.   struct eledata  *d = pd;      /* pointer to pd[i] */
  582.   REAL8 td, ti, ti2, tj1, tj2, tj3;
  583.  
  584.   if (thelup == jd_ad)
  585.     return; /* if already calculated then return */
  586.  
  587.   for (i = SUN; i <= MARS; i++, d++, e++) 
  588.   {
  589.     td = jd_ad - d->epoch;
  590.     ti = e->tj = td / 36525.0;  /* julian centuries from epoch */
  591.     ti2 = ti * ti;
  592.     tj1 = ti / 3600.0;  /* used when coefficients are in seconds of arc */
  593.     tj2 = ti * tj1;
  594.     tj3 = ti * tj2;
  595.     e->lg = mod8360(d->lg0 + d->lg1 * td  + d->lg2 * tj2 + d->lg3 * tj3);
  596.   
  597.     /* also with moon lg1 *td is exact to 10e-8 degrees within 5000 years */
  598.  
  599.     e->pe = mod8360(d->pe0 + d->pe1 * tj1 + d->pe2 * tj2 + d->pe3 * tj3);
  600.     e->ex = d->ex0 + d->ex1 * ti + d->ex2 * ti2;
  601.     e->kn = mod8360(d->kn0 + d->kn1 * tj1 + d->kn2 * tj2 + d->kn3 * tj3);
  602.     e->in = d->in0 + d->in1 * tj1 + d->in2 * tj2;
  603.     e->ma = smod8360(e->lg - e->pe);
  604.  
  605.     if (i == MOON) {
  606.       /* calculate ekliptic according Newcomb, APAE VI,
  607.       ** and nutation according Exp.Suppl. 1961, identical
  608.       ** with Mark Potttenger elemnut()
  609.       ** all terms >= 0.01" only .
  610.       ** The 1984 IAU Theory of Nutation, as published in
  611.       ** AE 1984 suppl. has not yet been implemented
  612.       ** because it would mean to use other elements of
  613.       ** moon and sun */
  614.  
  615.       REAL8 mnode, mlong2, slong2, mg, sg, d2;
  616.  
  617.       mnode  = DEGTORAD * e->kn;        /* moon's mean node */
  618.       mlong2 = DEGTORAD * 2.0 * e->lg;  /* 2 x moon's mean longitude */
  619.       mg     = DEGTORAD * e->ma;        /* moon's mean anomaly (g1) */
  620.       slong2 = DEGTORAD * 2.0 * ee->lg; /* 2 x sun's mean longitude (L), with
  621.                                         the phase 180 deg earth-sun irrelevant
  622.                                         because 2 x 180 = 360 deg */
  623.       sg     = DEGTORAD * ee->ma; /* sun's mean anomaly = earth's */
  624.       d2     = mlong2 - slong2;   /* 2 x elongation of moon from sun */
  625.       meanekl = ekld[0] + ekld[1] * tj1 + ekld[2] * tj2 + ekld[3] * tj3;
  626.       ekl = meanekl
  627.           + (9.2100 * COS8(mnode)
  628.           - 0.0904 * COS8(2.0 * mnode)
  629.           + 0.0183 * COS8(mlong2 - mnode)
  630.           + 0.0884 * COS8(mlong2)
  631.           + 0.0113 * COS8(mlong2 + mg)
  632.           + 0.5522 * COS8(slong2)
  633.           + 0.0216 * COS8(slong2 + sg)) / 3600.0;
  634.  
  635.       nut = ((-17.2327 - 0.01737 * ti) * SIN8(mnode)
  636.           + 0.2088 * SIN8(2.0 * mnode)
  637.           + 0.0675 * SIN8(mg)
  638.           - 0.0149 * SIN8(mg - d2)
  639.           - 0.0342 * SIN8(mlong2 - mnode)
  640.           + 0.0114 * SIN8(mlong2 - mg)
  641.           - 0.2037 * SIN8(mlong2)
  642.           - 0.0261 * SIN8(mlong2 + mg)
  643.           + 0.0124 * SIN8(slong2 - mnode)
  644.           + 0.0214 * SIN8(slong2 - sg)
  645.           - 1.2729 * SIN8(slong2)
  646.           - 0.0497 * SIN8(slong2 + sg)
  647.           + 0.1261 * SIN8(sg)) / 3600.0;
  648.     }
  649.   }
  650.  
  651.   /* calculate the arguments sa[] for the disturbation terms */
  652.  
  653.   ti = (jd_ad - EPOCH1850) / 365.25;  /* julian years from 1850 */
  654.   for (i = 0; i < SDNUM; i++)
  655.     sa [i] = mod8360(_sd [i].sd0 + _sd [i].sd1 * ti);
  656.  
  657.   /*
  658.   ** sa[2] += 0.3315 * SIN8 (DEGTORAD *(133.9099 + 38.39365 * el[SUN].tj));
  659.   **
  660.   ** correction of jupiter perturbation argument for sun from Pottenger;
  661.   ** creates only .03" and 1e-7 rad, not applied because origin unclear */
  662.  
  663.   thelup = jd_ad;               /* note the last helup time */
  664. }
  665.  
  666.  
  667. /*
  668. ** hel()
  669. ** Computes the heliocentric positions for all planets except the moon.
  670. ** The outer planets from Jupiter onwards, including Chiron, are 
  671. ** actually done by a subsequent call to outer_hel() which takes
  672. ** exactly the same parameters.
  673. ** hel() does true position relative to the mean ecliptic and equinox
  674. ** of date. Nutation is not added and must be done so by the caller.
  675. ** The latitude of the Sun (max. 0.5") is neglected and always returned
  676. ** as zero.
  677. ** 
  678. ** return: OK or ERR
  679. */
  680.  
  681. int hel(planet, t, al, ar, az, alp, arp, azp)
  682. int planet;   /* planet index as defined by placalc.h */
  683. REAL8 t;      /* relative juliand date, ephemeris time */
  684.               /* Now come 6 pointers to return values. */
  685. REAL8 *al;    /* longitude in degrees */
  686. REAL8 *ar;    /* radius in AU */
  687. REAL8 *az;    /* distance from ecliptic in AU */
  688. REAL8 *alp;   /* speed in longitude, degrees per day */
  689. REAL8 *arp;   /* speed in radius, AU per day */
  690. REAL8 *azp;   /* speed in z, AU per day */
  691. {
  692.   register struct elements *e;
  693.   register struct eledata  *d;
  694.   REAL8 lk = 0.0;
  695.   REAL8 rk = 0.0;
  696.   REAL8 b, h1, sini, sinv, cosi, cosu, cosv, man, truanom, esquare, 
  697.     k8, u, up, v, vp;
  698.  
  699.   if (planet >= JUPITER)
  700.     return (outer_hel(planet, t, al, ar, az, alp, arp, azp));
  701.  
  702.   if (planet < SUN || planet == MOON)
  703.     return ERR;
  704.  
  705.   e = &el[planet];
  706.   d = &pd[planet];
  707.   sini = SIN8(DEGTORAD * e->in);
  708.   cosi = COS8(DEGTORAD * e->in);
  709.   esquare = sqrt((1.0 + e->ex) / (1.0 - e->ex)); /* H6 in old version */
  710.   man = e->ma;
  711.  
  712.   if (planet == EARTH)  /* some longperiodic terms in mean longitude */
  713.   {
  714.     man += (0.266 * SIN8 (DEGTORAD * (31.8 + 119.0 * e->tj))
  715.       + 6.40 * SIN8(DEGTORAD * (231.19 + 20.2 * e->tj))
  716.       + (1.882-0.016*e->tj) * SIN8(DEGTORAD * (57.24 + 150.27 * e->tj))
  717.       ) / 3600.0;
  718.   }
  719.  
  720.   if (planet == MARS)  /* some longperiodic terms */
  721.   {
  722.     man += (0.606 * SIN8(DEGTORAD * (212.87 + e->tj * 119.051))
  723.       + 52.490 * SIN8(DEGTORAD * (47.48 + e->tj * 19.771))
  724.       +  0.319 * SIN8(DEGTORAD * (116.88 + e->tj * 773.444))
  725.       +  0.130 * SIN8(DEGTORAD * (74 + e->tj * 163))
  726.       +  0.280 * SIN8(DEGTORAD * (300 + e->tj * 40.8))
  727.       -  (37.05 +13.5 * e->tj)
  728.       ) / 3600.0;
  729.   }
  730.  
  731.   u = fnu (man, e->ex, 0.0000003); /* error 0.001" returns radians */
  732.   cosu = COS8(u);
  733.   h1 = 1 - e->ex * cosu;
  734.   *ar = d->axis * h1;
  735.   if (ABS8(rPi - u) < TANERRLIMIT)
  736.     truanom = u; /* very close to aphel */
  737.   else
  738.     truanom = 2.0 * ATAN8(esquare * TAN8(u * 0.5)); /* true anomaly, rad*/
  739.   v = smod8360(truanom * RADTODEG + e->pe - e->kn); /* argument of latitude */
  740.  
  741.   if (sini == 0.0 || ABS8(v -  90.0) < TANERRLIMIT
  742.                   || ABS8(v - 270.0) < TANERRLIMIT) 
  743.   {
  744.     *al = v;
  745.   } 
  746.   else 
  747.   {
  748.     *al = RADTODEG * ATAN8(TAN8(v * DEGTORAD) * cosi);
  749.     if (v > 90.0 && v < 270.0)  *al += 180.0;
  750.   }
  751.   *al = smod8360(*al + e->kn);
  752.   sinv = SIN8(v * DEGTORAD);
  753.   cosv = COS8(v * DEGTORAD);
  754.   *az = *ar * sinv * sini;
  755.   b = ASIN8(sinv * sini);     /* latitude in radians */
  756.   k8 = cosv / COS8(b) * sini;
  757.   up = 360.0 / d->period / h1;    /* du/dt degrees/day */
  758.  
  759.   if (ABS8(rPi - u) < TANERRLIMIT)
  760.     vp = up / esquare;  /* speed at aphel */
  761.   else
  762.     vp = up * esquare * (1 + COS8 (truanom)) / (1 + cosu);
  763.  
  764.   /* dv/dt degrees/day */
  765.   *arp = d->axis * up * DEGTORAD * SIN8(u) * e->ex;
  766.  
  767.   /* dr/dt AU/day */
  768.   *azp = *arp * sinv * sini + *ar * vp * DEGTORAD * cosv * sini;    /* dz/dt */
  769.   *alp = vp / cosi * (1 - k8 * k8);
  770.  
  771.   /* now come the disturbations */
  772.  
  773.   switch (planet) {
  774.     REAL8 am, mma, ema, u2;
  775.  
  776.   case EARTH:  
  777.     /* 
  778.     ** earth has some special moon values and a disturbation series due to the
  779.     ** planets. The moon stuff is due to the fact, that the mean elements
  780.     ** give the coordinates of the earth-moon barycenter. By adding the
  781.     ** corrections we effectively reduce to the center of the earth.
  782.     ** We neglect the correction in latitude, which is about 0.5", because
  783.     ** for astrological purposes we want the Sun to have latitude zero.
  784.     */
  785.     am = DEGTORAD * smod8360(el[MOON].lg - e->lg + 180.0); /* degrees */
  786.     mma = DEGTORAD * el[MOON].ma;
  787.     ema = DEGTORAD * e->ma;
  788.     u2 = 2.0 * DEGTORAD * (e->lg - 180.0 - el[MOON].kn); /* 2u' */
  789.  
  790.     lk = 6.454 * SIN8(am)
  791.        + 0.013 * SIN8(3.0 * am)
  792.        + 0.177 * SIN8(am + mma)
  793.        - 0.424 * SIN8(am - mma)
  794.        + 0.039 * SIN8(3.0 * am - mma)
  795.        - 0.064 * SIN8(am + ema)
  796.        + 0.172 * SIN8(am - ema)
  797.        - 0.013 * SIN8(am - mma - ema)
  798.        - 0.013 * SIN8(u2);
  799.  
  800.     rk = 13360 * COS8(am)
  801.        + 30    * COS8(3.0 * am)
  802.        + 370   * COS8(am + mma)
  803.        - 1330  * COS8(am - mma)
  804.        + 80    * COS8(3.0 * am - mma)
  805.        - 140   * COS8(am + ema)
  806.        + 360   * COS8(am - ema)
  807.        - 30    * COS8(am - mma - ema)
  808.        + 30    * COS8(u2);
  809.  
  810.     /* long periodic term from mars 15g''' - 8g'', Vol 6 p19, p24 */
  811.     lk += 0.202 * SIN8(DEGTORAD * (315.6 + 893.3 * e->tj));
  812.     disturb(earthkor, al, ar, lk, rk, man);
  813.     break;
  814.  
  815.   case MERCURY:  /* only normal disturbation series */
  816.     disturb(mercurykor, al, ar, 0.0, 0.0, man);
  817.     break;
  818.  
  819.   case VENUS:  /* some longperiod terms and normal series */
  820.     lk = (2.761 - 0.22*e->tj) * SIN8(DEGTORAD * (237.24 + 150.27 * e->tj))
  821.        + 0.269 * SIN8(DEGTORAD * (212.2  + 119.05 * e->tj))
  822.        - 0.208 * SIN8(DEGTORAD * (175.8  + 1223.5 * e->tj));
  823.  
  824.     /* make seconds */
  825.     disturb(venuskor, al, ar, lk, 0.0, man);
  826.     break;
  827.  
  828.   case MARS:  /* only normal disturbation series */
  829.     disturb(marskor, al, ar, 0.0, 0.0, man);
  830.     break;
  831.   }
  832.   return OK;
  833. }
  834.  
  835.  
  836. void disturb(k, al, ar, lk, rk, man)
  837. register struct kor *k;  /* ENDMARK-terminated array of struct kor */
  838. REAL8 *al, /* longitude in degrees, use a pointer to return value */
  839. *ar;       /* radius in AU */
  840. REAL8 lk,  /* longitude correction in SECONDS OF ARC */
  841.            /* function can be called with an lk and rk already
  842.               != 0, but no value is returned */
  843. rk,        /* radius correction in units of 9th place of log r */
  844. man;       /* mean anomaly of planet */
  845. {
  846.   REAL8 arg;
  847.   while (k->j != ENDMARK) 
  848.   {
  849.     arg = k->j * sa[k->k] + k->i * man;
  850.     lk += k->lampl * COS8(DEGTORAD * (k->lphase - arg));
  851.     rk += k->rampl * COS8(DEGTORAD * (k->rphase - arg));
  852.     k++;
  853.   }
  854.   *ar *= EXP10(rk * 1.0E-9);  /* 10^rk */
  855.   *al += lk / 3600.0;
  856. }
  857.  
  858.  
  859. int moon(al, ar, az)  /* return OK or ERR */
  860. REAL8 *al;
  861. REAL8 *ar;
  862. REAL8 *az;
  863. {
  864.   REAL8 a1,a2,a3,a4,a5,a6,a7,a8,a9,c2,c4,arg,b,d,f,dgc,dlm,dpm,dkm,dls;
  865.   REAL8 ca, cb, cd, f_2d, f_4d, g1c,lk,lk1,man,ms,nib,s,sinarg,sinp,sk;
  866.   REAL8 t, tb, t2c, r2rad, i1corr, i2corr, dlid;
  867.   int i;
  868.   struct elements *e;
  869.   struct m45dat   *mp;
  870.  
  871. #if MOON_TEST_CORR
  872.   struct m5dat    *m5p;
  873. #endif
  874.  
  875.   e = &el[MOON];
  876.   t = e->tj * 36525;  /* days from epoch 1900 */
  877.  
  878.   /* new format table II, parameters in full rotations of 360 degrees */
  879.  
  880.   r2rad = 360.0 * DEGTORAD;
  881.   tb  = t * 1e-12;  /* units of 10^12 */
  882.   t2c = t * t * 1e-16;  /* units of 10^16 */
  883.  
  884.   a1 = SIN8(r2rad * (0.53733431 -  10104982 * tb + 191 * t2c));
  885.   a2 = SIN8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  886.   c2 = COS8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  887.   a3 = SIN8(r2rad * (0.14222222 +   1536238 * tb));
  888.   a4 = SIN8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  889.   c4 = COS8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  890.   a5 = SIN8(r2rad * (0.52453688 - 147162675 * tb +  43 * t2c));
  891.   a6 = SIN8(r2rad * (0.84536324 -  11459387 * tb));
  892.   a7 = SIN8(r2rad * (0.23363774 +   1232723 * tb + 191 * t2c));
  893.   a8 = SIN8(r2rad * (0.58750000 +   9050118 * tb));
  894.   a9 = SIN8(r2rad * (0.61043085 -  67718733 * tb));
  895.  
  896.   dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4 + 0.237 * a6;
  897.   dpm = -2.1  * a3 - 2.076  * a2 - 0.840 * a4 - 0.593 * a6;
  898.   dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
  899.   dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
  900.   dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;  /* in revolutions */
  901.   dgc = (1.000002708 + 139.978 * dgc);  /* in this form used later */
  902.   man = DEGTORAD * (e->ma + (dlm - dpm) / 3600.0);
  903.  
  904.   /* man with periodic and secular corr. */
  905.  
  906.   ms  = DEGTORAD * (el[EARTH].ma + dls / 3600.0);
  907.   f   = DEGTORAD * (e->lg - e->kn + (dlm - dkm) / 3600.0);
  908.   d   = DEGTORAD * (e->lg + 180 - el[EARTH].lg + (dlm - dls) / 3600.0);
  909.  
  910.   lk = lk1 = sk = sinp = nib = g1c = 0;
  911.   i1corr = 1.0 - 6.8320E-8 * t;
  912.   i2corr = dgc * dgc; /* i2 occurs only as -2, 2 */
  913.  
  914.   for (i = 0, mp = m45; i < NUM_MOON_CORR; i++, mp++)
  915.   {
  916.     /* arg = mp->i0 * man + mp->i1 * ms + mp->i2 * f + mp->i3 * d; */
  917.     arg = mp->i0 * man;
  918.     arg += mp->i3 * d;
  919.     arg += mp->i2 * f;
  920.     arg += mp->i1 * ms;
  921.     sinarg = SIN8(arg);
  922.  
  923.     /*
  924.     ** now apply corrections due to changes in constants;
  925.     ** we correct only terms in l' (i1) and F (i2), not in l (i0), because
  926.     ** the latter are < 0.05"
  927.     ** We don't apply corrections  for cos(arg), i.e. for parallax
  928.     */
  929.  
  930.     if (mp->i1 != 0)   /* i1 can be -2, -1, 0, 1, 2 */
  931.     {
  932.       sinarg *= i1corr;
  933.       if  (mp->i1 == 2 || mp->i1 == -2)
  934.         sinarg *= i1corr;
  935.     }
  936.  
  937.     if (mp->i2 != 0)  /* i2 can be -2, 0, 2 */
  938.       sinarg *= i2corr;
  939.  
  940.     lk += mp->lng * sinarg;
  941.     sk += mp->lat * sinarg;
  942.     sinp += mp->par * COS8 (arg) ;
  943.   }
  944.  
  945. #if MOON_TEST_CORR  /* optionally add more lunar longitudes  */
  946.  
  947.   for (m5p = m5; m5p->i0 != 99; m5p++)   /* i0 = 99 is end mark */
  948.   {
  949.     arg = m5p->i0 * man + m5p->i1 * ms + m5p->i2 * f + m5p->i3 * d;
  950.     sinarg = SIN8(arg);
  951.     lk1 += m5p->lng * sinarg;
  952.   }  
  953. #endif
  954.  
  955.   /*
  956.   ** now compute some planetary terms in longitude, list i delta;
  957.   ** we take all > 0.5" and neglect secular terms in the arguments. These
  958.   ** produce phase errors > 10 degrees only after 3000 years.
  959.   */
  960.  
  961.   dlid =  0.822 * SIN8 (r2rad * (0.32480 - 0.0017125594 * t));
  962.   dlid += 0.307 * SIN8 (r2rad * (0.14905 - 0.0034251187 * t));
  963.   dlid += 0.348 * SIN8 (r2rad * (0.68266 - 0.0006873156 * t));
  964.   dlid += 0.662 * SIN8 (r2rad * (0.65162 + 0.0365724168 * t));
  965.   dlid += 0.643 * SIN8 (r2rad * (0.88098 - 0.0025069941 * t));
  966.   dlid += 1.137 * SIN8 (r2rad * (0.85823 + 0.0364487270 * t));
  967.   dlid += 0.436 * SIN8 (r2rad * (0.71892 + 0.0362179180 * t));
  968.   dlid += 0.327 * SIN8 (r2rad * (0.97639 + 0.0001734910 * t));
  969.  
  970.   /* without nutation */
  971.  
  972.   *al = smod8360(e->lg + (dlm + lk + lk1 + dlid) / 3600.0);
  973.  
  974.   /* solar Terms in latitude Nibeta */
  975.  
  976.   f_2d = f - 2.0 * d;
  977.   f_4d = f - 4.0 * d;
  978.  
  979.   nib += -526.069 * SIN8(                   f_2d);
  980.   nib +=   -3.352 * SIN8(                   f_4d);
  981.   nib +=   44.297 * SIN8( man             + f_2d);
  982.   nib +=   -6.000 * SIN8( man             + f_4d);
  983.   nib +=   20.599 * SIN8(-man             + f   );
  984.   nib +=  -30.598 * SIN8(-man             + f_2d);
  985.   nib +=  -24.649 * SIN8(-2*man           + f   );
  986.   nib +=   -2.000 * SIN8(-2*man           + f_2d);
  987.   nib +=  -22.571 * SIN8(          ms     + f_2d);
  988.   nib +=   10.985 * SIN8(         -ms     + f_2d);
  989.  
  990.   /* new gamma1C from 29 Jul 88, all terms > 0.4 " in table III, code 2 */
  991.  
  992.   g1c += -0.725 * COS8(          d);
  993.   g1c +=  0.601 * COS8(      2 * d);
  994.   g1c +=  0.394 * COS8(      3 * d);
  995.   g1c += -0.445 * COS8(man                   + 4 * d);
  996.   g1c +=  0.455 * COS8(man                   + 1 * d);
  997.   g1c +=  5.679 * COS8(2 * man               - 2 * d);
  998.   g1c += -1.300 * COS8(3 * man                      );
  999.   g1c += -1.302 * COS8(            ms               );
  1000.   g1c += -0.416 * COS8(            ms        - 4 * d);
  1001.   g1c += -0.740 * COS8(        2 * ms        - 2 * d);
  1002.   g1c +=  0.787 * COS8(    man +   ms        + 2 * d);
  1003.   g1c +=  0.461 * COS8(    man +   ms               );
  1004.   g1c +=  2.056 * COS8(    man +   ms        - 2 * d);
  1005.   g1c += -0.471 * COS8(    man +   ms        - 4 * d);
  1006.   g1c += -0.443 * COS8(   -man +   ms        + 2 * d);
  1007.   g1c +=  0.679 * COS8(   -man +   ms               );
  1008.   g1c += -1.540 * COS8(   -man +   ms        - 2 * d);
  1009.  
  1010.   s =  f + sk / 3600.0 * DEGTORAD;
  1011.   ca = 18519.7 + g1c;
  1012.   cb = -0.000336992 * ca * dgc * dgc * dgc;
  1013.   cd = ca / 18519.7;
  1014.   b = (ca * SIN8(s) * dgc  + cb * SIN8(3.0 * s) + cd * nib) / 3600.0;
  1015.  
  1016.   /* we neglect the planetary terms in latitude, code 4 in table III */
  1017.  
  1018.   sinp = (sinp + 3422.451); 
  1019.  
  1020.   /*
  1021.   ** Improved lunar ephemeris and APAE until ca. 1970 had here
  1022.   ** 3422.54 as constant of moon's sine parallax.
  1023.   ** The difference can be applied by direct addition of 0.089" to
  1024.   ** our parallax results.
  1025.   **
  1026.   ** To get the radius in A.U. from the sine parallax,
  1027.   ** we use 1964 IAU value 8.794" for solar parallax.
  1028.   ** sinp is still in seconds of arc.
  1029.   ** To calculate moon parallax in " it would be:
  1030.   ** p = sinp (1  + sinp * sinp * 3.917405E-12)
  1031.   ** based on the formula p = sinp + 1/6 sinp^3
  1032.   ** and taking into account the conversion of " to radians.
  1033.   ** The semidiameter of the moon is: (Expl.Suppl. 61, p 109)
  1034.   ** s = 0.0796 + 0.272446 * p
  1035.   */
  1036.  
  1037.   *ar = 8.794 / sinp;
  1038.   *az = *ar * SIN8(DEGTORAD * b);
  1039.   return OK;
  1040. }
  1041.  
  1042.  
  1043. /* solution of the Kepler equation, return rad */
  1044. /* t = mean anomaly in degrees                 */
  1045. /* ex = excentricity of orbit                  */
  1046. /* err = maximum error in degrees              */
  1047.  
  1048. REAL8 fnu(t, ex, err)
  1049. REAL8 t;
  1050. REAL8 ex;
  1051. REAL8 err;
  1052. {
  1053.   REAL8 u0, delta;
  1054.  
  1055.   t *= DEGTORAD;
  1056.   u0 = t;
  1057.   err *= DEGTORAD;
  1058.   delta = 1;
  1059.   while (ABS8(delta) >= err) 
  1060.   {
  1061.     delta = (t + ex * SIN8(u0) - u0) / (1 - ex * COS8(u0));
  1062.     u0 += delta;  
  1063.   }
  1064.   return u0;  
  1065. }
  1066.  
  1067.  
  1068. /*
  1069. ** outer_hel()
  1070. ** Computes the position of Jupiter, Saturn, Uranus, Neptune, Pluto and
  1071. ** Chiron by reading our stored ephemeris in steps of 80 (!) days and
  1072. ** applying a high order interpolation to it. The interpolation errors are
  1073. ** less than 0.01" seconds or arc.
  1074. ** The stored ephemeris is  packed in a special format consisting of
  1075. ** 32 bit numbers; it has been created on the Astrodienst Unix system
  1076. ** by numerical integration with routines provided originally by Marc
  1077. ** Pottenger, USA, which we improved for better long term precision.
  1078. ** Because the Unix system uses a different byte order than the MSDOS
  1079. ** systems, the bytes must be reordered for MSDOS after reading from
  1080. ** the binary files. 
  1081. **
  1082. ** outer_hel() takes the same parameters as hel().
  1083. ** It returns the same type of values.
  1084. **
  1085. ** The access to the ephemeris files is done in the functions chi_file_posit()
  1086. ** and lrz_file_posit().
  1087. */
  1088.  
  1089. int outer_hel(planet, jd_ad, al, ar, az, alp, arp, azp)
  1090. int planet;
  1091. REAL8 jd_ad;  /* jd_ad Astrodienst relative Julian ephemeris time */
  1092. REAL8 *al;
  1093. REAL8 *ar;
  1094. REAL8 *az;
  1095. REAL8 *alp;
  1096. REAL8 *arp;
  1097. REAL8 *azp;
  1098. {
  1099.   static FILE *outerfp = NULL, *chironfp = NULL;
  1100.   static double last_j0_outer = HUGE8;
  1101.   static double last_j0_chiron = HUGE8;
  1102.   static long  icoord[6][5][3], chicoord[6][3];
  1103.   REAL8 j0, jd, jfrac;
  1104.   REAL8 l[6], r[6], z[6];
  1105.   int n, order, p;
  1106.  
  1107.   if( planet < JUPITER || (planet > PLUTO && planet != CHIRON) )
  1108.     return ERR;
  1109.  
  1110.   jd = jd_ad + JUL_OFFSET;
  1111.   j0 = RFloor((jd - 0.5) / EPHE_STEP) * EPHE_STEP + 0.5;
  1112.   jfrac = (jd - j0) / EPHE_STEP;
  1113.  
  1114.   if (planet == CHIRON)
  1115.   {
  1116.     if (last_j0_chiron != j0)
  1117.     {
  1118.       for (n = 0; n < 6; n++)  /* read 6 days */
  1119.       {
  1120.         jd = j0 + (n - 2) * EPHE_STEP;
  1121.         if (chi_file_posit(jd, &chironfp) != OK)
  1122.           return (ERR);
  1123.  
  1124.         fread(&chicoord[n][0], sizeof(long), 3, chironfp);
  1125.         longreorder((UCHAR *)&chicoord[n][0], 3*4);
  1126.       }
  1127.       last_j0_chiron = j0;
  1128.     }
  1129.     for (n = 0; n < 6; n++) {
  1130.       l[n] = chicoord[n][0] / DEG2MSEC;
  1131.       r[n] = chicoord[n][1] / AU2INT;
  1132.       z[n] = chicoord[n][2] / AU2INT;
  1133.     }
  1134.   }
  1135.  
  1136.   else /* !CHIRON => an outerplanet */
  1137.   {
  1138.     if (last_j0_outer != j0)   /* read all 5 planets for 6 steps */
  1139.     {
  1140.       for (n = 0; n < 6; n++)
  1141.       {
  1142.         jd = j0 + (n - 2) * EPHE_STEP;
  1143.         if (lrz_file_posit(jd, &outerfp) != OK)
  1144.           return (ERR);
  1145.  
  1146.         fread(&icoord[n][0][0], sizeof(long), 15, outerfp);
  1147.         longreorder((UCHAR *)&icoord[n][0][0], 15*4);
  1148.       }
  1149.       last_j0_outer = j0;
  1150.     }
  1151.     p = planet - JUPITER;
  1152.     for (n = 0; n < 6; n++) {
  1153.       l[n] = icoord[n][p][0] / DEG2MSEC;
  1154.       r[n] = icoord[n][p][1] / AU2INT;
  1155.       z[n] = icoord[n][p][2] / AU2INT;
  1156.     }
  1157.   }
  1158.  
  1159.   if (planet > SATURN)
  1160.     order = 3;
  1161.   else
  1162.     order = 5;
  1163.  
  1164.   inpolq(2, order, jfrac, l, al, alp);    *alp /= EPHE_STEP;
  1165.   inpolq(2, order, jfrac, r, ar, arp);    *arp /= EPHE_STEP;
  1166.   inpolq(2, order, jfrac, z, az, azp);    *azp /= EPHE_STEP;
  1167.  
  1168.   return OK;
  1169. }
  1170.  
  1171.  
  1172. /*
  1173. ** quicker Everett interpolation, after Pottenger
  1174. ** version 9 Jul 1988 by Alois Treindl
  1175. ** return OK or ERR.
  1176. */
  1177.  
  1178. int inpolq(n, o, p, x, axu, adxu)
  1179. int n,     /* interpolate between x[n] and x[n-1], at argument n+p */
  1180. o;         /* order of interpolation, maximum 5 */
  1181. double p,  /* argument , intervall [0..1] */
  1182. x[],       /* array of function values, x[n-o]..x[n+o] must exist */
  1183. *axu,      /* pointer for storage of result */
  1184. *adxu;     /* pointer for storage of dx/dt  */
  1185. {
  1186.   static double q, q2, q3, q4, q5, p2, p3, p4, p5, u, u0, u1, u2;
  1187.   static double lastp = 9999.0;
  1188.  
  1189.   double dm2, dm1, d0, dp1, dp2,
  1190.     d2m1, d20, d2p1, d2p2, d30, d3p1, d3p2, d4p1, d4p2;
  1191.  
  1192.   double offset = 0.0;
  1193.  
  1194.   if (lastp != p)
  1195.   {
  1196.     q  = 1.0-p;
  1197.     q2 = q*q;
  1198.     q3 = (q+1.0)*q*(q-1.0)/6.0;       /* q - 1 over 3; u5 */
  1199.     p2 = p*p;
  1200.     p3 = (p+1.0)*p*(p-1.0)/6.0;       /* p - 1 over 3; u8 */
  1201.     u  = (3.0*p2-1.0)/6.0;
  1202.     u0 = (3.0*q2-1.0)/6.0;
  1203.     q4 = q2*q2;                       /* f5 */
  1204.     p4 = p2*p2;                       /* f4 */
  1205.     u1 = (5.0*p4-15.0*p2+4.0)/120.0;  /* u1 */
  1206.     u2 = (5.0*q4-15.0*q2+4.0)/120.0;  /* u2 */
  1207.     q5 = q3*(q+2.0)*(q-2.0)/20.0;     /* q - 2 over 5; u6 */
  1208.     p5 = (p+2.0)*p3*(p-2.0)/20.0;     /* p - 2 over 5; u9 */
  1209.     lastp = p;
  1210.   }
  1211.   dm1 = x[n] - x[n-1];
  1212.  
  1213.   if (dm1 > 180.0)    dm1 -= 360.0;
  1214.   if (dm1 < -180.0)   dm1 += 360.0;
  1215.  
  1216.   d0 = x[n+1] - x[n];
  1217.  
  1218.   if (d0 >  180.0)  {  d0 -= 360.0;   offset =  360.0; }
  1219.   if (d0 < -180.0)  {  d0 += 360.0;   offset = -360.0; }
  1220.  
  1221.   dp1 = x[n+2] - x[n+1];
  1222.  
  1223.   if (dp1 >  180.0)   dp1 -= 360.0;
  1224.   if (dp1 < -180.0)   dp1 += 360.0;
  1225.  
  1226.   d20  = d0 - dm1;    /* f8 */
  1227.   d2p1 = dp1 - d0;    /* f9 */
  1228.  
  1229.   /* Everett interpolation 3rd order */
  1230.  
  1231.   *axu = q*(x[n] + offset) + q3*d20 + p*x[n+1] + p3*d2p1;
  1232.   *adxu = d0 + u*d2p1 - u0*d20;
  1233.  
  1234.   if (o > 3)     /* 5th order */
  1235.   {
  1236.     dm2 = x[n-1] - x[n-2];
  1237.     if (dm2 >  180.0)      dm2 -= 360.0;
  1238.     if (dm2 < -180.0)      dm2 += 360.0;
  1239.  
  1240.     dp2 = x[n+3] - x[n+2];
  1241.  
  1242.     if (dp2 >  180.0)      dp2 -= 360.0;
  1243.     if (dp2 < -180.0)      dp2 += 360.0;
  1244.  
  1245.     d2m1 = dm1  - dm2;
  1246.     d2p2 = dp2  - dp1;
  1247.     d30  = d20  - d2m1;
  1248.     d3p1 = d2p1 - d20;
  1249.     d3p2 = d2p2 - d2p1;
  1250.     d4p1 = d3p1 - d30;     /* f7 */
  1251.     d4p2 = d3p2 - d3p1;    /* f */
  1252.  
  1253.     *axu  += p5*d4p2 + q5*d4p1;
  1254.     *adxu += u1*d4p2 - u2*d4p1;
  1255.   }
  1256.   return OK;
  1257. }
  1258.  
  1259.  
  1260. /*
  1261. ** position lrz file at proper position for julian date jd; 
  1262. ** Return OK or ERR.  Version for outer planets.
  1263. ** The path where the ephemeris files are looked for is defined
  1264. ** by ephe_path.
  1265. */
  1266.  
  1267. int lrz_file_posit(jd, lrzfpp)
  1268.  
  1269. double jd;     /* full Julian day number, not Astrodienst relative */
  1270.  
  1271. FILE **lrzfpp; /* pointer to file pointer; this function
  1272.                   opens or closes the ephemeris file, and caller
  1273.                   should keep it open while using it. The caller
  1274.                   should close it when he is finished with using
  1275.                   the placalc() package. */
  1276. {
  1277.   int filenr;
  1278.   long posit, jlong;
  1279.   static char fname[80];
  1280.   static int open_lrznr = -10000; /* local memory to remember whether
  1281.                                      an already open file is the one with
  1282.                                      the correct number for this date */
  1283.  
  1284.   jlong = (long)RFloor(jd); 
  1285.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1286.  
  1287.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1288.     filenr--;
  1289.  
  1290.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1291.   posit = (posit / (int)EPHE_STEP) * EPHE_OUTER_BSIZE;
  1292.  
  1293.   if (*lrzfpp == NULL || open_lrznr != filenr)  /* no or wrong open file */
  1294.   {
  1295.     open_lrznr = -10000;
  1296.     sprintf(fname, "%s%s%d", EPHE_OUTER, filenr < 0 ? "M" : "", abs(filenr));
  1297.  
  1298.     if (*lrzfpp != NULL)
  1299.       fclose(*lrzfpp);
  1300.  
  1301.     *lrzfpp = FileOpen(fname, 2);
  1302.  
  1303.     if (*lrzfpp == NULL)
  1304.     {
  1305.       fprintf(stderr, "Placalc: File %s does not exist.\n", fname);
  1306.       return (ERR);
  1307.     }
  1308.     open_lrznr = filenr;
  1309.   }
  1310.   if (fseek(*lrzfpp, posit, 0) == 0)
  1311.     return (OK);
  1312.  
  1313.   fprintf(stderr, "Placalc: Seek error file %s position %ld\n", fname, posit);
  1314.   return ERR; /* this fseek error occurs only with incomplete files */
  1315. }
  1316.  
  1317.  
  1318. /*
  1319. ** position chiron file at proper position for julian date jd;
  1320. ** Return OK or ERR. Version for Chiron.
  1321. ** Sister function to lrz_file_posit().
  1322. */
  1323.  
  1324. int chi_file_posit(jd, lrzfpp)
  1325.  
  1326. double jd;  /* full Julian day number, not Astrodienst relative */
  1327.  
  1328. FILE **lrzfpp; /* pointer to file pointer; this function
  1329.                   opens or closes the ephemeris file, and caller
  1330.                   should keep it open while using it */
  1331. {
  1332.   int filenr;
  1333.   long posit, jlong;
  1334.   char fname[80];
  1335.  
  1336.   static int open_lrznr = -10000; /* local memory to remember whether
  1337.                                      an already open file is the one with
  1338.                                      the correct number for this date */
  1339.   jlong = (long)RFloor(jd);
  1340.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1341.  
  1342.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1343.     filenr--;
  1344.  
  1345.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1346.   posit = (posit / (int)EPHE_STEP) * EPHE_CHIRON_BSIZE;
  1347.  
  1348.   if (*lrzfpp == NULL || open_lrznr != filenr)  /* no or wrong open file */
  1349.   {
  1350.     open_lrznr = -10000;
  1351.     sprintf(fname, "%s%s%d", EPHE_CHIRON, filenr < 0 ? "M" : "", abs(filenr));
  1352.  
  1353.     if (*lrzfpp != NULL)
  1354.       fclose(*lrzfpp);
  1355.  
  1356.     *lrzfpp = FileOpen(fname, 2);
  1357.  
  1358.     if (*lrzfpp == NULL)
  1359.     {
  1360.       fprintf(stderr, "Placalc: File %s does not exist.\n", fname);
  1361.       return ERR;
  1362.     }
  1363.     open_lrznr = filenr;
  1364.   }
  1365.   if (fseek (*lrzfpp, posit, 0) == 0)
  1366.     return OK;
  1367.  
  1368.   fprintf(stderr, "Placalc: Seek error file %s position %ld\n", fname, posit);
  1369.   return ERR; /* this fseek error occurs only with incomplete files */
  1370. }
  1371.  
  1372.  
  1373. /* x MOD 360.0, so that x in 0..360 */
  1374.  
  1375. REAL8 smod8360(x)
  1376. REAL8 x;
  1377. {
  1378.   while (x >= 360.0)
  1379.     x -= 360.0;
  1380.   while (x < 0.0)
  1381.     x += 360.0;
  1382.   return x;
  1383. }
  1384.  
  1385.  
  1386. /* x MOD 360.0, so that x in 0..360 */
  1387.  
  1388. REAL8 mod8360(x)
  1389. REAL8 x;
  1390. {
  1391.   if (x >= 0 && x < 360.0)
  1392.     return x;
  1393.   return x - 360.0 * RFloor(x / 360.0);
  1394. }
  1395.  
  1396.  
  1397. /* a - b on a 360 degree circle, result -180..180 */
  1398.  
  1399. REAL8 diff8360(a, b) 
  1400. REAL8 a;
  1401. REAL8 b;
  1402. {
  1403.   REAL8 d;
  1404.  
  1405.   d = a - b;
  1406.   if (d >= 180.0)    return d - 360.0;
  1407.   if (d < -180.0)    return d + 360.0;
  1408.   return d;
  1409. }
  1410.  
  1411.  
  1412. REAL8 test_near_zero(x)
  1413. REAL8 x;
  1414. {
  1415.   if (ABS8(x) >= NEAR_ZERO)
  1416.     return x;
  1417.  
  1418.   if (x < 0)
  1419.     return -NEAR_ZERO;
  1420.  
  1421.   return NEAR_ZERO;
  1422. }
  1423.  
  1424.  
  1425. /*
  1426. ** p points to memory filled with long values; for
  1427. ** each of the values the seqeuence of the four bytes
  1428. ** has to be reversed, to translate HP-UX and VAX
  1429. ** ordering to MSDOS/Turboc ordering
  1430. */
  1431.  
  1432. void longreorder(p, n) 
  1433. UCHAR *p;
  1434. int n;
  1435. {
  1436.   int i;
  1437.   unsigned char c0, c1, c2, c3;
  1438.   static int orderinit = 0;
  1439.   unsigned short test;
  1440.  
  1441.   if (!orderinit) 
  1442.   {
  1443.     test = 0x0001;
  1444.     orderinit = (*(unsigned char *)(&test)) ? 1 : -1;
  1445.   }
  1446.  
  1447.   if (orderinit < 0)
  1448.     return;
  1449.  
  1450.   for (i = 0; i < n; i += 4, p += 4)
  1451.   {
  1452.     c0 = *p;
  1453.     c1 = *(p + 1);
  1454.     c2 = *(p + 2);
  1455.     c3 = *(p + 3);
  1456.     *p = c3;
  1457.     *(p + 1) = c2;
  1458.     *(p + 2) = c1;
  1459.     *(p + 3) = c0;
  1460.   }
  1461. }
  1462. #endif /* PLACALC */
  1463.  
  1464. /* placalc.c */
  1465.